package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;
@Service
@Transactional
public class OerderSetingServiceimpl implements OerderSetingService {
    @Autowired
    OrderSettingDao orderSettingDao;
    @Override
    public void edit(List<OrderSetting> orderSettingList) {
       if (orderSettingList!=null&&orderSettingList.size()>0){
           for (OrderSetting orderSetting : orderSettingList) {
               OrderSetting orderSetting1=orderSettingDao.findByDate(orderSetting.getOrderDate());
               if (orderSetting1==null){
                   orderSettingDao.add(orderSetting);
               }else {
                   if (orderSetting1.getReservations()>orderSetting.getNumber()){
                       throw new RuntimeException("可预约人数小于已预约人数,不能修改,批量导入失败!!");
                   }else {
                       orderSettingDao.update(orderSetting);
                   }
               }
           }

       }

    }

    @Override
    public List<OrderSetting> findByMonth(String month) {
        String firstday=month+"-01";
        String lastday=month+"-31";
        List<OrderSetting>orderSettingList=orderSettingDao.findOrderSerttings(firstday,lastday);
        return orderSettingList;
    }
}
